package com.zxp.mybatis.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zxp
 * @date 2021-08-25 9:52
 */
@ToString
@AllArgsConstructor @NoArgsConstructor
public class Page<T> implements Serializable {

    @Getter@Setter
    private int pageNo = 1;//页码，默认是第一页

    @Getter@Setter
    private int pageSize = 15;//每页显示的记录数，默认是15

    private int totalRecord;//总记录数

    @Getter@Setter
    private int totalPage;//总页数

    @Getter@Setter
    private List<T> results;//对应的当前页记录

    @Getter@Setter
    private Map<String, Object> params = new HashMap<String, Object>();//其他的参数我们把它分装成一个Map对象

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
        //在设置总页数的时候计算出对应的总页数，在下面的三目运算中加法拥有更高的优先级，所以最后可以不加括号。
        int totalPage = totalRecord%pageSize==0 ? totalRecord/pageSize : totalRecord/pageSize + 1;
        this.setTotalPage(totalPage);
    }

    public Page(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }
}
